//-----------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (C) Copyright IBM Corp. 2010, 2011 All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with
// IBM Corp.
//-----------------------------------------------------------------

/**
 *    used for paging tools
 */

PageUtils = {
	id: "",
	hasInit:false,
	currentContextID: "",
    currentControllerID: "",
    totalPages: "",
    pageSize: "",
    properties: [],
    //init2:function(){alert('ok')},
    init:function(contextID, controllerID, totalP, pSize){
	  
	 if (trim(contextID) != "" && trim(controllerID) != ""){
			if(wc.render.getRefreshControllerById(controllerID)
					&&wc.render.getContextById(contextID) ){
				 
				this.currentContextID = contextID;
				this.currentControllerID =controllerID;
				this.totalPages = totalP;
				this.pageSize = pSize;
				this.hasInit = true; 
			}
		}
 
		return this.hasInit;
	},

	/**
	 * go to the given page number
	 * @param pageNum
	 * @param dataUrl
	 * @return
	 */
	gotoPage: function(pageNum, dataUrl, phId) {
		if (phId) {
			var hp = this.finePagingHelper(phId);
			if (hp) {
				 
				return hp.gotoPage(pageNum, dataUrl);
			}
		}
		if(!submitRequest()){
			return;
		}  
		 
		if(!this.hasInit) { 
			return;
		}
		//alert('pageNum='+ pageNum);
		//alert('pageSize='+ this.pageSize);
		if (!pageNum)  {
			return;
		}
		try{
			if(!parseInt(pageNum))
				return; 
			pageNum = pageNum<=this.totalPages?pageNum:this.totalPages;
			pageNum = pageNum<=0?1:pageNum; 
			 
			var beginIndex = new String((pageNum-1)*this.pageSize);
			//if (wc.render.getContextById(this.currentContextID).properties['beginIndex']){ 
				wc.render.getContextById(this.currentContextID).properties['beginIndex']= beginIndex;
			//}
			
			//if (wc.render.getContextById(this.currentContextID).properties['searchResultsPageNum']){
				wc.render.getContextById(this.currentContextID).properties['searchResultsPageNum'] = beginIndex;
			 
			//}
			 
			var loadUrl = dataUrl; 
			//TODO: parse and validate the url
			if(loadUrl.indexOf("?")>-1) {
				loadUrl += "&amp;selectPage="+pageNum; 
				//loadUrl += "&amp;beginIndex="+beginIndex; 
			} else {
				loadUrl += "?selectPage="+pageNum; 
				//loadUrl += "&amp;beginIndex="+beginIndex; 
			} 
			
			cursor_wait();
			CommonControllersDeclarationJS.setControllerURL(this.currentControllerID,loadUrl);	
			//alert(loadUrl);
			 
			wc.render.updateContext(this.currentContextID);
			 
			 
		} catch(e) {
			//handle exception
			cursor_clear();
		}
		
	},
	
	callback: function (f) {
		return f;
	},
	
	finePagingHelper: function(hid) {
		if (pageHelperMap[hid]) {
			return pageHelperMap[hid];
		} else {
			return null;
		}
			 
	}

},


pageHelperMap = [];

PagingUtils = function(config){
		if (config && typeof config =="object") {
			this.hasInit = true;
			this.id = config.id; 		
			this.currentContextID = config.contextId;
			this.currentControllerID = config.controllerId;
			this.totalPages = config.totalPages;
			this.pageSize =config.pageSize;
			this.properties = config.properties;
			if (pageHelperMap[config.id]==undefined){
				pageHelperMap[config.id]=this;
			}
		} else {
			this.hasInit = false;
			this.id = ""; 		
			this.currentContextID = null;
			this.currentControllerID = null;
			this.totalPages = null;
			this.pageSize = null;
			this.properties = null;
		}

	    
	    //init2:function(){alert('ok')},
	    this.init = function(contextID, controllerID, totalP, pSize){
		  
		 if (trim(contextID) != "" && trim(controllerID) != ""){
				if(wc.render.getRefreshControllerById(controllerID)
						&&wc.render.getContextById(contextID) ){
					 
					this.currentContextID = contextID;
					this.currentControllerID =controllerID;
					this.totalPages = totalP;
					this.pageSize = pSize;
					this.hasInit = true; 
				}
			}
	 
			return this.hasInit;
		};
		this.initFromConfig = function() {
			if (config && typeof config == "object") {
				if (trim(config.contextId) != "" && trim(config.controllerId) != ""){
					if(wc.render.getRefreshControllerById(config.controllerId)
							&&wc.render.getContextById(config.contextId) ){
					
						this.currentContextID = config.contextId;
						this.currentControllerID = config.controllerId;
						this.totalPages = config.totalPages;
						this.pageSize = config.pageSize;
						this.hasInit = true; 
					}
				
				}
			}
			return this.hasInit;
		}
		/**
		 * go to the given page number
		 * @param pageNum
		 * @param dataUrl
		 * @return
		 */
		this.gotoPage = function(pageNum, dataUrl) {
			 
			if(!submitRequest()){
				return;
			}  
			 
			if(!this.hasInit) { 
				return;
			}
			//alert('2~~pageNum='+ pageNum);
			//alert('this.totalPages='+ this.totalPages);
			if (!pageNum)  {
				return;
			}
			try{
				if(!parseInt(pageNum))
					return; 
				pageNum = pageNum<=this.totalPages?pageNum:this.totalPages;
				pageNum = pageNum<=0?1:pageNum; 
				 
				var beginIndex = new String((pageNum-1)*this.pageSize);
				//if (wc.render.getContextById(this.currentContextID).properties['beginIndex']){ 
					wc.render.getContextById(this.currentContextID).properties['beginIndex']= beginIndex;
				//}
				
				//if (wc.render.getContextById(this.currentContextID).properties['searchResultsPageNum']){
					wc.render.getContextById(this.currentContextID).properties['searchResultsPageNum'] = beginIndex;
				 
				//}
				// alert("pageNum =" + pageNum);
				var loadUrl = dataUrl;  
				//TODO: parse and validate the url
				if(loadUrl.indexOf("?")>-1) {
					loadUrl += "&amp;selectPage="+pageNum; 
					//loadUrl += "&amp;beginIndex="+beginIndex; 
				} else {
					loadUrl += "?selectPage="+pageNum; 
					//lloadUrloadUrl += "&amp;beginIndex="+beginIndex; 
				} 
				//alert(loadUrl);
				//alert(this.currentContextID);
				cursor_wait();
				CommonControllersDeclarationJS.setControllerURL(this.currentControllerID,loadUrl);	
				//alert(loadUrl);
				 
				wc.render.updateContext(this.currentContextID);
				 
				 
			} catch(e) {
				//handle exception
				cursor_clear();
			}
			
		};
		
		this.callback =  function (f) {
			return f;
		};
		this.createInstance = function(config) { return this.hasInit;
			//obj = new PagingUtils(config);
			//return obj;
		}
	};

